package com.nic.view.adapter;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;

import com.nic.view.R;
import com.nic.view.model.CheckType;
import com.nic.view.model.CheckViewHolder;
import com.nic.view.model.CommonChooseBean;


/**
 * Created by Administrator on 2018/4/18.
 */

public class SingleChooseAdapter extends CommonCheckAdapter<CommonChooseBean, CheckViewHolder> {
    public SingleChooseAdapter() {
        super(CheckType.SINGLE);
    }

    @Override
    public CheckViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choose, null);
        final CheckBox checkBox = (CheckBox) rootView.findViewById(R.id.cb_single);
        return new CheckViewHolder(rootView) {
            @Override
            public CheckBox create() {
                return checkBox;
            }
        };
    }

    @Override
    public void onBindViewHolder(ViewGroup parent, CheckViewHolder holder, int position) {
        CommonChooseBean bean = getList().get(position);
        holder.create().setText("" + bean.getName());
    }

    @Override
    public String getItemBeanId(int position) {
        return getList().get(position).getId();
    }

    @Override
    public String getItemBeanName(int position) {
        return getList().get(position).getName();
    }

    @Override
    public void initCheckBox(ViewGroup parent, CheckBox checkBox) {
        super.initCheckBox(parent, checkBox);
        checkBox.setTextColor(ContextCompat.getColorStateList(parent.getContext(), R.color.tv_white_gray));
        checkBox.setBackgroundResource(R.drawable.select_single_choose1);
        checkBox.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));// 通过透明度的方式.
    }

    /**
     * 选中项预设
     *
     * @param currentId
     */
    public CommonChooseBean checked(String currentId) {
        CommonChooseBean checkBean = null;
        if (!getList().isEmpty()) {
            checkBean = getList().get(0);//默认选中项
            if (!TextUtils.isEmpty(currentId)) {
                for (int i = 0; i < getList().size(); i++) {
                    CommonChooseBean item = getList().get(i);
                    if (currentId.equals(item.getId())) {
                        setSelected(i);
                        return item;
                    }
                }
            }
        }
        return checkBean;
    }

}
